package com.rongc.client.freight.business.mine.view.activity;

import android.text.Html;
import android.view.View;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.rongc.client.core.network.code.HandleCode;
import com.rongc.client.core.network.http.RequestManager;
import com.rongc.client.core.utils.DateUtil;
import com.rongc.client.core.utils.LogUtils;
import com.rongc.client.freight.R;
import com.rongc.client.freight.UniApplication;
import com.rongc.client.freight.base.BaseActivity;
import com.rongc.client.freight.base.model.UserBean;
import com.rongc.client.freight.base.request.result.UserResult;
import com.rongc.client.freight.business.mine.request.api.MarkApi;
import com.rongc.client.freight.business.mine.request.api.MyMarkApi;
import com.rongc.client.freight.utils.UtilMethod;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by ccqx on 2017/6/28.
 */

public class MarkActivity extends BaseActivity implements View.OnClickListener{
    @Bind(R.id.tv_score)
    TextView mTvScore;
    @Bind(R.id.tv_continuous)
    TextView mTvContinuous;
    @Bind(R.id.tv_mark)
    TextView mTvMark;
    @Bind(R.id.tv_tip)
    TextView mTvTip;
    @Bind(R.id.tv_rule)
    TextView mTvRule;

    Response.Listener<JSONObject> respMyMarkListener = new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            if (HandleCode.requestSuccess()) {
                mTvScore.setText(Html.fromHtml(String.format(getResources().getString(R.string.mark_score), response.optString("score"))));
                mTvContinuous.setText(String.format(getResources().getString(R.string.mark_continuous),response.optString("number")));
                mTvTip.setText(Html.fromHtml(String.format(getResources().getString(R.string.mark_tip),
                        response.optString("number_sy"),response.optString("score_jl"))));
                mTvRule.setText(response.optString("remark"));
                showMark(response.optString("is_qiandao").equals("1"));
            } else {
                UtilMethod.dismissProgressDialog(MarkActivity.this);
            }
        }
    };


    Response.Listener<UserResult> respOrderListener = new Response.Listener<UserResult>() {

        @Override
        public void onResponse(UserResult response) {
            if (HandleCode.requestSuccess()) {
                initData();
            } else {
                UtilMethod.dismissProgressDialog(MarkActivity.this);
            }
        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            LogUtils.i("error.getMessage(): " + error.getMessage());
            UtilMethod.dismissProgressDialog(MarkActivity.this);
        }
    };

    @OnClick({R.id.tv_mark})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_mark:
                RequestManager.getInstance().call(new MarkApi(
                        new MarkApi.MarkParams(),
                        respOrderListener, errorListener));
                break;
        }
    }

    @Override
    public void initData() {
        RequestManager.getInstance().call(new MyMarkApi(
                new MyMarkApi.MyMarkParams(),
                respMyMarkListener, errorListener));
    }

    void showMark(boolean con){
        if(con){
            mTvMark.setBackgroundResource(R.drawable.universal_shape_btn_corner_white);
            mTvMark.setTextColor(getResources().getColor(R.color.universal_text_secondary));
            mTvMark.setText(R.string.mark_btn_sel);
            mTvMark.setEnabled(false);
        }else{
            mTvMark.setBackgroundResource(R.drawable.universal_shape_btn_corner_app);
            mTvMark.setTextColor(getResources().getColor(R.color.universal_text_white));
            mTvMark.setText(R.string.mark_btn_nor);
            mTvMark.setEnabled(true);
        }
    }

    @Override
    public void initView() {
        initToolbar(R.string.mark_title);
        UserBean bean = UniApplication.getInstance().getUserInfo();

        showMark(DateUtil.getCurrentDate(DateUtil.dateFormatYMD).equals(bean.getMarkTime()));

        mTvScore.setText(Html.fromHtml(String.format(getResources().getString(R.string.mark_score),bean.getIntegral())));
        mTvContinuous.setText(String.format(getResources().getString(R.string.mark_continuous),bean.getMarkCount()));
        mTvTip.setText(Html.fromHtml(String.format(getResources().getString(R.string.mark_tip),"6","300积分")));
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_mark;
    }
}
